/*
 * 类名称:FireExtinguisherDetailController.java
 * 包名称:com.platform.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-09-25 19:51:14        hww     初版做成
 *
 * Copyright (c) 2019-2019 微同科技
 */
package com.platform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.annotation.LoginUser;
import com.platform.annotation.SysLog;
import com.platform.entity.*;
import com.platform.service.EnterpriseInspectorService;
import com.platform.utils.PageUtils;
import com.platform.utils.R;
import com.platform.controller.AbstractController;
import com.platform.service.FireExtinguisherDetailService;
import com.platform.utils.excel.ExcelExport;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 灭火器详情（检查记录）Controller
 *
 * @author hww
 * @date 2019-09-25 19:51:14
 */
@RestController
@RequestMapping("fs/fireextinguisherdetail")
public class FireExtinguisherDetailController extends AbstractController {
    @Autowired
    private FireExtinguisherDetailService fireExtinguisherDetailService;

    @Autowired
    private EnterpriseInspectorService enterpriseInspectorService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return R
     */
    @RequestMapping("/queryAll")
//    @RequiresPermissions("fs:fireextinguisherdetail:list")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<FireExtinguisherDetailEntity> list = fireExtinguisherDetailService.queryAll(params);

        return R.ok().put("list", list);
    }

    /**
     * 分页查询灭火器详情（检查记录）
     *
     * @param params 查询参数
     * @return R
     */
    @GetMapping("/list")
//    @RequiresPermissions("fs:fireextinguisherdetail:list")
    public R list(@RequestParam Map<String, Object> params) {
//        Page page = fireExtinguisherDetailService.queryPage(params);
//
//        return R.ok().put("page", page);
        Page page = fireExtinguisherDetailService.queryPage(params);

        List<FireExtinguisherDetailEntity> list = page.getRecords();
        int total = (int)page.getTotal();//;
        int pageNum = (int)page.getCurrent();//
        int limit = (int)page.getSize();
        PageUtils pageUtil = new PageUtils(list,total,limit,pageNum);
        return R.ok().put("page", pageUtil);

    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return R
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("fs:fireextinguisherdetail:info")
    public R info(@PathVariable("id") String id) {
        FireExtinguisherDetailEntity fireExtinguisherDetail = fireExtinguisherDetailService.getById(id);

        return R.ok().put("fireExtinguisherDetail", fireExtinguisherDetail);
    }

    /**
     * 新增灭火器详情（检查记录）
     *
     * @param fireExtinguisherDetail fireExtinguisherDetail
     * @return R
     */
    @SysLog("新增灭火器详情（检查记录）")
    @RequestMapping("/save")
//    @RequiresPermissions("fs:fireextinguisherdetail:save")
    public R save(@RequestBody FireExtinguisherDetailEntity fireExtinguisherDetail) {
//        SysUserEntity sysUser = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
//        String mobile = sysUser.getMobile();
//
//        Map<String,Object> queryParams = new HashMap<>();
//        queryParams.put("mobile",mobile);
//        EnterpriseInspectorEntity enterpriseInspectorEntity = new EnterpriseInspectorEntity();
//        List<EnterpriseInspectorEntity> enterpriseInspectorEntities = enterpriseInspectorService.queryAll(queryParams);
//        if(enterpriseInspectorEntities.size()>0) {
//            enterpriseInspectorEntity = enterpriseInspectorEntities.get(0);
//            fireExtinguisherDetail.setInspectUser(enterpriseInspectorEntity.getId());
//            fireExtinguisherDetail.setInspectUserName(enterpriseInspectorEntity.getName());
//        }else{
////            return R.error();
//        }

        fireExtinguisherDetail.setInspectUser("1");
        fireExtinguisherDetail.setInspectUserName("黄伟伟");

        fireExtinguisherDetail.setId(UUID.randomUUID().toString());
        fireExtinguisherDetail.setInspectTime(new Date());

        fireExtinguisherDetailService.add(fireExtinguisherDetail);

        return R.ok();
    }

    /**
     * 修改灭火器详情（检查记录）
     *
     * @param fireExtinguisherDetail fireExtinguisherDetail
     * @return R
     */
    @SysLog("修改灭火器详情（检查记录）")
    @RequestMapping("/update")
//    @RequiresPermissions("fs:fireextinguisherdetail:update")
    public R update(@RequestBody FireExtinguisherDetailEntity fireExtinguisherDetail) {
        fireExtinguisherDetailService.update(fireExtinguisherDetail);

        return R.ok();
    }

    /**
     * 根据主键删除灭火器详情（检查记录）
     *
     * @param ids ids
     * @return R
     */
    @SysLog("删除灭火器详情（检查记录）")
    @RequestMapping("/delete")
    @RequiresPermissions("fs:fireextinguisherdetail:delete")
    public R delete(@RequestBody String[] ids) {
        fireExtinguisherDetailService.deleteBatch(ids);

        return R.ok();
    }


    /**
     * 导出灭火器详情
     */
    @RequestMapping("/export")
//    @RequiresPermissions("user:export")
    public R export(@RequestParam Map<String, Object> params, HttpServletResponse response) {

        List<FireExtinguisherDetailEntity> fireExtinguisherDetailEntities = fireExtinguisherDetailService.queryAll(params);

        ExcelExport ee = new ExcelExport("灭火器详情");

        String[] header = new String[]{"检查时间", "巡查员姓名", "状态", "备注", "图片", "位置"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (fireExtinguisherDetailEntities != null && fireExtinguisherDetailEntities.size() != 0) {
            for (FireExtinguisherDetailEntity fed : fireExtinguisherDetailEntities) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("inspectTime", fed.getInspectTime());
                map.put("inspectUserName", fed.getInspectUserName());
                map.put("status", fed.getStatus().equals("1")?"正常":"异常");
                map.put("remark", fed.getRemark());
                map.put("picUrl", fed.getPicUrl());
                map.put("position", fed.getPosition());
                list.add(map);
            }
        }

        ee.addSheetByMap("灭火器详情", list, header);
        ee.export(response);
        return R.ok();
    }
}
